package vn.edu.saigontech.ncms.web.bookstore;

import vn.edu.saigontech.ncms.service.ItemService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateItemAction extends AbstractValidator {
	private Long id;
	private Long titleId;
	private Long typeId;
	private String itemId;
	private String price;
	
	@Override
	public String check() {
		if(Checker.isNull(this.titleId)) {
			this.sendKeyError("validator.required", "bookstore.title");
		}
		if(Checker.isNull(this.typeId)) {
			this.sendKeyError("validator.required", "bookstore.item.type");
		}
		if (Checker.isEmpty(this.itemId)) {
			this.sendKeyError("validator.required", "bookstore.item.id");
		}
		if (ItemService.INSTANCE.findById(this.id) == null) {
			if(ItemService.INSTANCE.findByItemId(this.itemId) != null) {
				this.sendKeyError("validator.exist", "bookstore.item");
			}
		}
		if(Checker.isEmpty(this.price)) {
			this.sendKeyError("validator.required", "bookstore.item.price");
		}
		if(!Checker.isDouble(this.price)) {
			this.sendKeyError("validator.invalid", "bookstore.item.price");
		}
		return this.toSuccess();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getTitleId() {
		return titleId;
	}

	public void setTitleId(Long titleId) {
		this.titleId = titleId;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}
}
